﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Facebook;
using Newtonsoft.Json.Linq;
using System.IO.IsolatedStorage;

namespace AOuSuisJeWP7
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            
            fbLogin1.OnSuccess += new EventHandler(fbLogin1_OnSuccess);

        }
        
        FacebookClient FbCl;
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
        }

        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Profile.xaml", UriKind.Relative));
        }

        private void ApplicationBarMenuItem_Click_1(object sender, EventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings.Remove("login");
            ((App)Application.Current).userLogin= null;
           NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Participate.xaml", UriKind.Relative));
        }

        private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
        {
            // La navigation ne devrait s'effectuer que si l'utilisateur est loggué

            NavigationService.Navigate(new Uri("/NewEnigma.xaml", UriKind.Relative));
        }

        private void FbFriends_Click(object sender, RoutedEventArgs e)
        {
            if (((App)Application.Current).userLogin.FbToken == null)
            {
                //NavigationService.Navigate(new Uri("/FbPage.xaml", UriKind.Relative));
                FbFriends.Visibility = System.Windows.Visibility.Collapsed;
                Friends.Visibility = System.Windows.Visibility.Collapsed;
                fbLogin1.IsEnabled = true;
                fbLogin1.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                GetFbFriends(((App)Application.Current).userLogin.FbToken);
            }
        }

        void fbLogin1_OnSuccess(object sender, EventArgs e)
        {
            FacebookOAuthResult oauthResult = (FacebookOAuthResult)sender;
            ((App)App.Current).userLogin.FbToken = oauthResult.AccessToken;
            IsolatedStorageSettings.ApplicationSettings["login"]= ((App)App.Current).userLogin;
            GetFbFriends(oauthResult.AccessToken);
        }
        void GetFbFriends(string token)
        {
            FbCl= new Facebook.FacebookClient(token);
            FbCl.GetCompleted += new EventHandler<FacebookApiEventArgs>(cl_GetCompleted);
            FbCl.GetAsync("/me/friends");
        }

        void cl_GetCompleted(object sender, FacebookApiEventArgs e)
        { 
            Dispatcher.BeginInvoke(() =>
           {
                fbLogin1.Visibility = Visibility.Collapsed;
                FbFriends.Visibility = System.Windows.Visibility.Collapsed;
                Friends.Visibility = System.Windows.Visibility.Visible;
                var json = JObject.Parse(((JsonObject)e.GetResultData()).ToString())["data"];
                var names =json.Select(elem=>elem["name"]);

                Friends.listBox1.DataContext = names;
               
           });
            //throw new NotImplementedException();
        }
    }
}